package com.lindroy.androidplayer.ui.home.fragment

import android.os.Bundle
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.google.android.material.tabs.TabLayout
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseLazyFragment
import com.lindroy.androidplayer.extensions.*
import com.lindroy.androidplayer.ui.knowledge.KnowledgeIndexFragment
import com.lindroy.androidplayer.ui.knowledge.KnowledgeQuestionFragment
import com.lindroy.androidplayer.ui.knowledge.NavigationFragment
import com.lindroy.lib_utils.extensions.getResString
import kotlinx.android.synthetic.main.fragment_knowledge.*
import kotlinx.android.synthetic.main.fragment_official_account.*
import kotlinx.android.synthetic.main.tablayout.*

/**
 * @author Lin
 * @date 2021/4/28
 * @function 首页-体系总页面
 */
class KnowledgeFragment(override val layoutId: Int = R.layout.fragment_knowledge) :
    BaseLazyFragment() {

    private val adapter by lazy {
        TabAdapter(childFragmentManager)
    }

    private val tabs =
        arrayOf(
            getResString(R.string.knowledge_system),
            getResString(R.string.navigation),
            getResString(R.string.q_and_a)
        )

    override fun initView() {
        vpKnowledge.offscreenPageLimit = tabs.size
        vpKnowledge.adapter = adapter
        tabLayout.setupWithViewPager(vpKnowledge)
    }

    override fun lazyLoad() {
    }

    override fun initListener() {
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.let { vpKnowledge.currentItem = it.position }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })

    }


    override fun initClick() {

    }

    companion object {
        @JvmStatic
        fun newInstance() =
            KnowledgeFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }

    inner class TabAdapter(fm: FragmentManager) :
        FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

        private val fragments = arrayOf(
            KnowledgeIndexFragment.newInstance(),
            NavigationFragment.newInstance(),
            KnowledgeQuestionFragment.newInstance()
        )

        override fun getCount(): Int = tabs.size

        override fun getItem(position: Int) = fragments[position]

        //显示标签上的文字
        override fun getPageTitle(position: Int): CharSequence = tabs[position]
    }
}